package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.client.ItemFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @author: atguigu
 * @create: 2022-12-29 11:42
 */
@Controller
public class ItemController {

    @Autowired
    private ItemFeignClient itemFeignClient;

    /**
     * 渲染门户首页中详情详情页
     *
     * @param skuId 商品SKUID
     * @return
     */
    @GetMapping("/{skuId}.html")
    public String getItem(@PathVariable("skuId") Long skuId, Model model) {
        //1.获取详情页模板/item/item.html文件渲染所需要数据
        Result<Map<String, Object>> result = itemFeignClient.getBySkuId(skuId);
        //2.通过Model设置数据模型
        model.addAllAttributes(result.getData());

        //3.返回模板页面 底层：通过模板引擎将数据跟页面合并，渲染html文件
        return "item/item";
    }
}
